//
//  Date_Extension.swift
//  timeManager
//
//  Created by 钟志远 on 2019/11/17.
//  Copyright © 2019 钟志远. All rights reserved.
//

import Foundation

public extension Date {
    func startOfDay(timeZone: TimeZone = TimeZone.current) -> Date {
        var dateComponents = Calendar(identifier: Calendar.Identifier.gregorian).dateComponents(in: timeZone, from: self)
        dateComponents.setValue(0, for: Calendar.Component.hour)
        dateComponents.setValue(0, for: Calendar.Component.minute)
        dateComponents.setValue(0, for: Calendar.Component.second)
        return dateComponents.date ?? self
    }
    
    func endOfDay(timeZone: TimeZone = TimeZone.current) -> Date {
        var dateComponents = Calendar(identifier: Calendar.Identifier.gregorian).dateComponents(in: timeZone, from: self)
        dateComponents.setValue(23, for: Calendar.Component.hour)
        dateComponents.setValue(59, for: Calendar.Component.minute)
        dateComponents.setValue(59, for: Calendar.Component.second)
        return dateComponents.date ?? self
    }
    
    func todayInterval() -> Int {
        let components = Calendar(identifier: Calendar.Identifier.gregorian).dateComponents([.hour, .minute], from: self)
        if let hour = components.hour, let min = components.minute {
            return hour * 60 + min
        }
        return 0
    }
    
    func weekDayStartWithZero() -> Int? {
        let components = Calendar(identifier: Calendar.Identifier.gregorian).dateComponents([.weekday], from: self)
        
        if var weekday = components.weekday {
            weekday -= 2
            if weekday == -1 {
                weekday = 6
            }
            return weekday
        }
        return nil
    }
    
    func daysOfMonth() -> Int {
        let components = Calendar(identifier: Calendar.Identifier.gregorian)
        let range = components.range(of: .day, in: .month, for: self)
        return range?.count ?? 0
        
    }
    
    func weekDateInterval(timeZone: TimeZone = .current) -> (Date, Date)? {
        let calender = Calendar(identifier: Calendar.Identifier.gregorian)
        guard let weekDay = weekDayStartWithZero() else {
            return nil
        }
        
        var monday: Date?
        var sunday: Date?
        if weekDay != 0 {
           var d = DateComponents()
            d.weekday = 2
            monday = calender.nextDate(after: self, matching: d, matchingPolicy: .nextTime, repeatedTimePolicy: .first, direction: .backward)
        } else {
           monday = self
        }
        
        if weekDay != 6 {
            var d = DateComponents()
            d.weekday = 1
            sunday = calender.nextDate(after: self, matching: d, matchingPolicy: Calendar.MatchingPolicy.nextTime)
        } else {
            sunday = self
        }
        

        guard let m = monday, let s = sunday else {
            return nil
        }
        
        return (start: m.startOfDay(), end: s.endOfDay())
    }
}

public extension Date {
    
    func getTimeIntervalDesc(dateFormater: DateFormatter) -> String{
        var interval = self.timeIntervalSinceNow
        interval = interval < 0 ? -interval : interval
        
        switch  interval {
        case let x where x < 3600: // 小于一小时
            let min = Int(x / 60)
            if min == 0 {
                return "刚刚"
            } else{
                return min.description + "分钟前"
            }
        case let x where x < 3600 * 24 : //小于一天
            let hour = Int(x / 3600)
            return hour.description + "小时前"
        case let x where x < 3600 * 24 * 2: //昨天
            return "昨天"
        default:
            dateFormater.dateFormat = "yyyy-MM-dd"
            return dateFormater.string(from: self)
        }
    }
    
    func getTimeRemain(dateFormater: DateFormatter) -> (day: String, hour: String, min: String)? {
        var interval = Int(self.timeIntervalSinceNow)
        if interval <= 0 {
            return nil
        }
        
        var min = 0
        var hour = 0
        var day = 0
        
        day =  interval / (3600 * 24)
        interval = interval % (3600 * 24)
        hour = interval / 3600
        interval = interval % (3600)
        min = interval / 60
        
        let strMin = String.init(format: "%02d", min)
        let strHour = String.init(format: "%02d", hour)
        let strDay = String.init(format: "%2d", day)
        
        return (strDay, strHour, strMin)
    }
    
    static func getHourMinWithInterval(interval: Int) -> (hour: Int, min: Int) {
        var interval = interval
        let hour = interval / 3600
        interval = interval % 3600
        let min = interval / 60
        return (hour, min)
    }
}
